<?php
class ControllerCommunityPost extends Controller{
    private $error=array();
    private $topicInfo=array();
    private $postInfo=array();

    public function index(){
        $this->redirect($this->url->link('error/not_found'));
  	}

    //post a topic
    public function topic(){
        //detect if user login
        if(!$this->customer->isLogged()){
            $this->redirect($this->url->link('account/login', '', 'SSL'));
        }

        //if $_GET['fid'] does no exists or is empty or is not numeric, redirect
        if(!isset($this->request->get['fid']) || empty($this->request->get['fid']) || !is_numeric($this->request->get['fid'])){
            $this->redirect($this->url->link('error/not_found'));
        }

        $this->load->model('community/user');
        $this->load->model('community/post');
        $this->language->load('community/post');
        $this->language->load('community/community_header');

        $userPermission=$this->model_community_user->getPermission($this->request->get,$this->customer->getForumUserType(),$this->customer->getForumUserRank());

        $forumResult=$this->model_community_post->getForumInfo($this->request->get);

        //if it is not forum but a forum cat or it is not a valid form id ,redirect
        if($forumResult['forum_type']=='0' || empty($forumResult)){
            $this->redirect($this->url->link('error/not_found'));
        }

        //if forum disabled, redirect
        if(!$forumResult['forum_enabled']){
            $this->session->data['forwarded_link']=$this->url->link('community/community','','SSL');
            $this->session->data['warning_type']='not_allow_view_forum';
            $this->redirect($this->url->link('community/warning','','SSL'));
        }

        //if do not have permission to post topic
        if(!$userPermission['topic_publish']){
            $this->session->data['forwarded_link']=$this->url->link('community/viewforum','fid=' . $this->request->get['fid'],'SSL');
            $this->session->data['warning_type']='not_allow_action';
            $this->redirect($this->url->link('community/warning','','SSL'));
        }

        $this->document->setTitle($this->language->get('text_post_topic'));

        //submit
        if(isset($this->request->post['submit']) && $this->validate($userPermission)){
            $this->session->data['previous_post_time']=time();

            if($userPermission['posting_approval']){
                $postingApproval=TRUE;
            }else{
                $postingApproval=FALSE;
            }

            $topicId=$this->model_community_post->addTopic($this->request->get,$this->request->post,$postingApproval);

            if(isset($this->request->get['did'])){
                $this->model_community_post->deleteDraft($this->request->get);
            }

            if($postingApproval){
                $this->session->data['forwarded_link']=$this->url->link('community/viewforum','fid=' . $this->request->get['fid'],'SSL');
                $this->session->data['success_type']='post_waiting_approval';
            }else{
                $this->session->data['topic_link']=$this->url->link('community/viewtopic','fid=' . $this->request->get['fid'] . '&tid=' . $topicId,'SSL');
                $this->session->data['success_type']='topic';
            }

            //detect posting forum point hostory
            $point_histories=$this->model_community_post->getLatestPostingForumPointAwardingTime();

            $previous_posting_count=0;

            foreach($point_histories as $point_history){
                if($point_history['add_time']>=strtotime('today')){
                    $previous_posting_count++;
                }
            }

            if($previous_posting_count<$this->config->get('daily_awarded_posting_count') && $this->customer->getMasterSlave()!='sl'){
                $this->model_community_post->addForumPoints();

                $this->session->data['text_forum_points_awarded']=sprintf($this->language->get('text_forum_points_awarded'),$this->config->get('posting_awarded_forum_points'));
            }

            $this->redirect($this->url->link('community/success','','SSL'));
        }

        //save draft submit
        if(isset($this->request->post['savedraft']) && $this->validate($userPermission)){
            $this->session->data['previous_post_time']=time();

            $this->model_community_post->saveDraft($this->request->get,$this->request->post);

            $this->session->data['forwarded_link']=$this->url->link('community/viewforum','fid=' . $this->request->get['fid'],'SSL');
            $this->session->data['success_type']='draft';
            $this->redirect($this->url->link('community/success','','SSL'));
        }

        //get topic category
        if (isset($this->request->post['topic-category'])) {
            $this->data['topic_category_id'] = $this->request->post['topic-category'];
        } else {
            $this->data['topic_category_id'] = '';
        }

        $results=$this->model_community_post->getForumTopicCats($this->request->get);
        $this->data['topic_cats']=array();

        foreach($results as $result){
            $this->data['topic_cats'][]=array(
                'topic_category_id'      =>      $result['topic_category_id'],
                'topic_category_title'   =>      $result['topic_category_title']
            );
        }

        //shared prefix
        $draftLink=isset($this->request->get['did']) ? '&did=' . $this->request->get['did'] : '';

        $this->sharedPrefix($this->url->link('community/viewforum') . '&fid=' . $this->request->get['fid'], $forumResult['forum_name'], $this->url->link('community/post/topic', 'fid=' . $this->request->get['fid'] . $draftLink, 'SSL'), $userPermission);

        $this->data['text_headline_name']=$forumResult['forum_name'];
        $this->data['text_post']=$this->language->get('text_post_topic');

        $this->data['headline_link']=$this->url->link('community/viewforum') . '&fid=' . $this->request->get['fid'];

        //load Posting Options when posting a topic
        if($this->request->post){
            $this->data['is_enable_bbcode']=isset($this->request->post['disable_bbcode']) && $this->request->post['disable_bbcode'] ? 'checked="checked"' : '';
            $this->data['is_enable_smilies']=isset($this->request->post['disable_smilies']) && $this->request->post['disable_smilies'] ? 'checked="checked"' : '';
            $this->data['is_enable_magic_url']=isset($this->request->post['not_parse_urls']) && $this->request->post['not_parse_urls'] ? 'checked="checked"' : '';
            $this->data['is_enable_sig']=isset($this->request->post['attach_signature']) && $this->request->post['attach_signature'] ? 'checked="checked"' : '';
            $this->data['is_reply_notify']=isset($this->request->post['notify_me']) && $this->request->post['notify_me'] ? 'checked="checked"' : '';
        }else{
            $this->data['is_enable_bbcode']=$this->customer->getForumUserAttachBbcode() ? '' : 'checked="checked"';
            $this->data['is_enable_smilies']=$this->customer->getForumUserAttachSmiles() ? '' : 'checked="checked"';
            $this->data['is_enable_sig']=$this->customer->getForumUserAttachSignature() ? 'checked="checked"' : '';
            $this->data['is_reply_notify']=$this->customer->getForumUserAttachNotify() ? 'checked="checked"' : '';
        }

        //load poll details when posting a topic
        if(isset($this->request->post['submit'])){
            $this->data['poll_question']=$this->request->post['poll-question'];
            $this->data['poll_option']=$this->request->post['poll-option'];
            $this->data['poll_option_per_user']=$this->request->post['poll-option-per-user'];
            $this->data['poll_run_day']=$this->request->post['poll-run-day'];
        }else{
            $this->data['poll_question']='';
            $this->data['poll_option']='';
            $this->data['poll_option_per_user']='';
            $this->data['poll_run_day']='';
        }

        //detect enabled or disabled
        $this->data['topic_category_enabled']=$forumResult['topic_category_enabled'];

        if( $userPermission['poll_publish']){
            $this->data['allow_poll']=TRUE;
        }else{
            $this->data['allow_poll']=FALSE;
        }

        if($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['topic_featured']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['topic_featured'])){
            $this->data['allow_feature_topic']=TRUE;
        }else{
            $this->data['allow_feature_topic']=FALSE;
        }

        if($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['topic_lock']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['topic_lock'])){
            $this->data['allow_lock_topic']=TRUE;
        }else{
            $this->data['allow_lock_topic']=FALSE;
        }

        if($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['edit_lock']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['edit_lock'])){
            $this->data['allow_lock_edit']=TRUE;
        }else{
            $this->data['allow_lock_edit']=FALSE;
        }

        if($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['topic_sticky']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['topic_sticky'])){
            $this->data['allow_topic_sticky']=TRUE;
        }else{
            $this->data['allow_topic_sticky']=FALSE;
        }

        if($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['topic_announcement']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['topic_announcement'])){
            $this->data['allow_topic_announcement']=TRUE;
        }else{
            $this->data['allow_topic_announcement']=FALSE;
        }

        if($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['topic_global_announcement']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['topic_global_announcement'])){
            $this->data['allow_topic_global_announcement']=TRUE;
        }else{
            $this->data['allow_topic_global_announcement']=FALSE;
        }

        //shared appendix
        $this->sharedAppendix();
    }

    // post a reply
    public function reply(){
        //detect if user login
        if(!$this->customer->isLogged()){
            $this->redirect($this->url->link('account/login', '', 'SSL'));
        }

        //if $_GET['fid'] does no exists or is empty or is not numeric, redirect
        if(!isset($this->request->get['fid']) || empty($this->request->get['fid']) || !is_numeric($this->request->get['fid'])){
            $this->redirect($this->url->link('error/not_found'));
        }

        //if $_GET['tid'] does no exists or is empty or is not numeric, redirect
        if(!isset($this->request->get['tid']) || empty($this->request->get['tid']) || !is_numeric($this->request->get['tid'])){
            $this->redirect($this->url->link('error/not_found'));
        }

        $this->load->model('community/user');
        $this->load->model('community/post');
        $this->language->load('community/post');
        $this->language->load('community/community_header');

        $userPermission=$this->model_community_user->getPermission($this->request->get,$this->customer->getForumUserType(),$this->customer->getForumUserRank());

        $forumResult=$this->model_community_post->getForumInfo($this->request->get);

        $topicResult=$this->model_community_post->replyGetTopicInfo($this->request->get);
        $this->topicInfo=array(
            'topic_title'      =>  $topicResult['topic_title'],
            'topic_featured'   =>  $topicResult['topic_featured'],
            'topic_type'       =>  $topicResult['topic_type'],
            'topic_time_limit' =>  $topicResult['topic_time_limit'],
            'topic_status'     =>  $topicResult['topic_status']
        );

        //if it is not forum but a forum cat or it is not a valid form id ,redirect
        if($forumResult['forum_type']=='0' || empty($forumResult)){
            $this->redirect($this->url->link('error/not_found'));
        }

        //if topic is locked or tid is not valid ,redirect
        if($topicResult['topic_status']=='1' || empty($topicResult)){
            $this->session->data['forwarded_link']=$this->url->link('community/viewforum','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'],'SSL');
            $this->session->data['warning_type']='not_allow_action';
            $this->redirect($this->url->link('community/warning','','SSL'));
        }

        //if does not have permission to post reply
        if(!$userPermission['topic_reply']){
            $this->session->data['forwarded_link']=$this->url->link('community/viewforum','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'],'SSL');
            $this->session->data['warning_type']='not_allow_action';
            $this->redirect($this->url->link('community/warning','','SSL'));
        }

        //submit
        if(isset($this->request->post['submit']) && $this->validate($userPermission)){
            $this->session->data['previous_post_time']=time();

            if($userPermission['posting_approval']){
                $postingApproval=TRUE;
            }else{
                $postingApproval=FALSE;
            }

            $this->model_community_post->addReply($this->request->get,$this->request->post,$postingApproval);

            if(isset($this->request->get['did'])){
                $this->model_community_post->deleteDraft($this->request->get);
            }

            if($postingApproval){
                $this->session->data['forwarded_link']=$this->url->link('community/viewtopic','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'],'SSL');
                $this->session->data['success_type']='post_waiting_approval';
            }else{
                $this->session->data['reply_link']=$this->url->link('community/viewtopic','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'],'SSL');
                $this->session->data['success_type']='reply';
            }

            //detect posting forum point hostory
            $point_histories=$this->model_community_post->getLatestPostingForumPointAwardingTime();

            $previous_posting_count=0;

            foreach($point_histories as $point_history){
                if($point_history['add_time']>=strtotime('today')){
                    $previous_posting_count++;
                }
            }

            if($previous_posting_count<$this->config->get('daily_awarded_posting_count') && $this->customer->getMasterSlave()!='sl'){
                $this->model_community_post->addForumPoints();

                $this->session->data['text_forum_points_awarded']=sprintf($this->language->get('text_forum_points_awarded'),$this->config->get('posting_awarded_forum_points'));
            }

            $this->redirect($this->url->link('community/success','','SSL'));
        }

        //save draft submit
        if(isset($this->request->post['savedraft']) && $this->validate($userPermission)){
            $this->session->data['previous_post_time']=time();

            $this->model_community_post->saveDraft($this->request->get,$this->request->post);

            $this->session->data['forwarded_link']=$this->url->link('community/viewtopic','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'],'SSL');
            $this->session->data['success_type']='draft';
            $this->redirect($this->url->link('community/success','','SSL'));
        }

        //shared prefix
        $draftLink=isset($this->request->get['did']) ? '&did=' . $this->request->get['did'] : '';

        $this->sharedPrefix($this->url->link('community/viewforum').'&fid='.$this->request->get['fid'], $forumResult['forum_name'], $this->url->link('community/post/reply', 'fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'] . $draftLink, 'SSL'), $userPermission);

        $this->document->setTitle($this->language->get('text_post_reply'));

        $this->data['headline_link']=$this->url->link('community/viewtopic','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'],'SSL');

        $this->data['topic_type']=$topicResult['topic_type'];
        $this->data['text_headline_name']=$topicResult['topic_title'];
        $this->data['text_post']=$this->language->get('text_post_reply');

        //load Posting Options when adding a reply
        if($this->request->post){
            $this->data['is_enable_bbcode']=isset($this->request->post['disable_bbcode']) && $this->request->post['disable_bbcode'] ? 'checked="checked"' : '';
            $this->data['is_enable_smilies']=isset($this->request->post['disable_smilies']) && $this->request->post['disable_smilies'] ? 'checked="checked"' : '';
            $this->data['is_enable_magic_url']=isset($this->request->post['not_parse_urls']) && $this->request->post['not_parse_urls'] ? 'checked="checked"' : '';
            $this->data['is_enable_sig']=isset($this->request->post['attach_signature']) && $this->request->post['attach_signature'] ? 'checked="checked"' : '';
            $this->data['is_reply_notify']=isset($this->request->post['notify_me']) && $this->request->post['notify_me'] ? 'checked="checked"' : '';
        }else{
            $this->data['is_enable_bbcode']=$this->customer->getForumUserAttachBbcode() ? '' : 'checked="checked"';
            $this->data['is_enable_smilies']=$this->customer->getForumUserAttachSmiles() ? '' : 'checked="checked"';
            $this->data['is_enable_sig']=$this->customer->getForumUserAttachSignature() ? 'checked="checked"' : '';
            #topic_id selected from community_topic_notify table
            $this->data['is_reply_notify']=(isset($topicResult['topic_id']) || $this->customer->getForumUserAttachNotify()) ? 'checked="checked"' : '';
        }

        //detect enabled or disabled
        $this->data['allow_feature_topic']=FALSE;

        $this->data['allow_lock_topic']=FALSE;

        if($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['edit_lock']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['edit_lock'])){
            $this->data['allow_lock_edit']=TRUE;
        }else{
            $this->data['allow_lock_edit']=FALSE;
        }

        $this->data['allow_topic_sticky']=FALSE;

        $this->data['allow_topic_announcement']=FALSE;

        $this->data['allow_topic_global_announcement']=FALSE;

        //shared appendix
        $this->sharedAppendix();
    }
    
    // edit a post
    public function edit(){
        //detect if user login
        if(!$this->customer->isLogged()){
            $this->redirect($this->url->link('account/login', '', 'SSL'));
        }

        //if $_GET['fid'] does no exists or is empty or is not numeric, redirect
        if(!isset($this->request->get['fid']) || empty($this->request->get['fid']) || !is_numeric($this->request->get['fid'])){
            $this->redirect($this->url->link('error/not_found'));
        }

        //if $_GET['tid'] does no exists or is empty or is not numeric, redirect
        if(!isset($this->request->get['tid']) || empty($this->request->get['tid']) || !is_numeric($this->request->get['tid'])){
            $this->redirect($this->url->link('error/not_found'));
        }

        //if $_GET['pid'] does no exists or is empty or is not numeric, redirect
        if(!isset($this->request->get['pid']) || empty($this->request->get['pid']) || !is_numeric($this->request->get['pid'])){
            $this->redirect($this->url->link('error/not_found'));
        }

        $this->load->model('community/user');
        $this->load->model('community/post');
        $this->language->load('community/post');
        $this->language->load('community/community_header');

        $userPermission=$this->model_community_user->getPermission($this->request->get,$this->customer->getForumUserType(),$this->customer->getForumUserRank());

        $forumResult=$this->model_community_post->getForumInfo($this->request->get);

        $topicResult=$this->model_community_post->editGetTopicInfo($this->request->get);
        $this->topicInfo=array(
            'topic_title'      =>  $topicResult['topic_title'],
            'topic_featured'   =>  $topicResult['topic_featured'],
            'topic_type'       =>  $topicResult['topic_type'],
            'topic_time_limit' =>  $topicResult['topic_time_limit'],
            'topic_status'     =>  $topicResult['topic_status']
        );

        $postResult=$this->model_community_post->getPostInfo($this->request->get);
        $this->postInfo=array(
            'poster_id'         =>  $postResult['poster_id'],
            'post_subject'      =>  $postResult['post_subject'],
            'post_text'         =>  $postResult['post_text'],
            'enable_bbcode'     =>  $postResult['enable_bbcode'],
            'enable_smilies'    =>  $postResult['enable_smilies'],
            'enable_magic_url'  =>  $postResult['enable_magic_url'],
            'enable_sig'        =>  $postResult['enable_sig'],
            'topic_id'          =>  $postResult['topic_id'],
            'post_edit_reason'  =>  $postResult['post_edit_reason'],
            'post_edit_locked'  =>  $postResult['post_edit_locked']
        );

        //if it is not forum but a forum cat or it is not a valid form id ,redirect
        if($forumResult['forum_type']=='0' || empty($forumResult)){
            $this->redirect($this->url->link('error/not_found'));
        }

        //if tid is not valid ,redirect
        if(empty($topicResult)){
            $this->session->data['forwarded_link']=$this->url->link('community/viewforum','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'],'SSL');
            $this->session->data['warning_type']='not_allow_action';
            $this->redirect($this->url->link('community/warning','','SSL'));
        }

        //if it is a locked topic and do not have permission to edit, redirect
        if(!($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['post_members_edit']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['post_members_edit'])) && $topicResult['topic_status']=='1'){
            $this->session->data['forwarded_link']=$this->url->link('community/viewforum','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'],'SSL');
            $this->session->data['warning_type']='not_allow_action';
            $this->redirect($this->url->link('community/warning','','SSL'));
        }

        //if editor is not the author or does not have mod editing permission, redirect
        if(!($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['post_members_edit']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['post_members_edit']) || (!$this->postInfo['post_edit_locked'] && $this->postInfo['poster_id']==$this->customer->getForumUserId() && $userPermission['post_edit']))){
            $this->session->data['forwarded_link']=$this->url->link('community/viewforum','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'],'SSL');
            $this->session->data['warning_type']='not_allow_action';
            $this->redirect($this->url->link('community/warning','','SSL'));
        }

        //submit edit
        if(isset($this->request->post['submit']) && $this->validate($userPermission)){
            $this->session->data['previous_post_time']=time();

            if($userPermission['posting_approval']){
                $postingApproval=TRUE;
            }else{
                $postingApproval=FALSE;
            }

            $this->model_community_post->editPost($this->request->get,$this->request->post,$postingApproval);

            if($postingApproval){
                $this->session->data['forwarded_link']=$this->url->link('community/viewforum','fid=' . $this->request->get['fid'],'SSL');
                $this->session->data['success_type']='post_waiting_approval';
            }else{
                $this->session->data['edit_link']=$this->url->link('community/viewtopic','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'],'SSL');
                $this->session->data['success_type']='edit';
            }

            $this->redirect($this->url->link('community/success','','SSL'));
        }

        //shared prefix
        $this->sharedPrefix($this->url->link('community/viewforum').'&fid='.$this->request->get['fid'], $forumResult['forum_name'], $this->url->link('community/post/edit', 'fid='.$this->request->get['fid'].'&tid='.$this->request->get['tid'].'&pid='.$this->request->get['pid'], 'SSL'), $userPermission);

        $this->document->setTitle($this->language->get('text_post_edit'));

        $this->data['text_headline_name']=$topicResult['topic_title'];
        $this->data['topic_type']=$topicResult['topic_type'];
        $this->data['topic_first_post_id']=$topicResult['topic_first_post_id'];
        $this->data['text_post']=$this->language->get('text_post_edit');

        $this->data['headline_link']=$this->url->link('community/viewtopic','fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'],'SSL');

        //get topic categories
        if (isset($this->request->post['topic-category'])) {
            $this->data['topic_category_id'] = $this->request->post['topic-category'];
        } else {
            if($topicResult['topic_category_id']){
                $this->data['topic_category_id'] = $topicResult['topic_category_id'];
            }else{
                $this->data['topic_category_id'] = '';
            }
        }

        $results=$this->model_community_post->getForumTopicCats($this->request->get);

        $this->data['topic_cats']=array();

        foreach($results as $result){
            $this->data['topic_cats'][]=array(
                'topic_category_id'      =>      $result['topic_category_id'],
                'topic_category_title'   =>      $result['topic_category_title']
            );
        }

        //load Posting Options when edit a post
        if($this->request->post){
            $this->data['is_enable_bbcode']=isset($this->request->post['disable_bbcode']) && $this->request->post['disable_bbcode'] ? 'checked="checked"' : '';
            $this->data['is_enable_smilies']=isset($this->request->post['disable_smilies']) && $this->request->post['disable_smilies'] ? 'checked="checked"' : '';
            $this->data['is_enable_magic_url']=isset($this->request->post['not_parse_urls']) && $this->request->post['not_parse_urls'] ? 'checked="checked"' : '';
            $this->data['is_enable_sig']=isset($this->request->post['attach_signature']) && $this->request->post['attach_signature'] ? 'checked="checked"' : '';
            $this->data['is_reply_notify']=isset($this->request->post['notify_me']) && $this->request->post['notify_me'] ? 'checked="checked"' : '';
        }else{
            $this->data['is_enable_bbcode']=$this->postInfo['enable_bbcode'] ? '' : 'checked="checked"';
            $this->data['is_enable_smilies']=$this->postInfo['enable_smilies'] ? '' : 'checked="checked"';
            $this->data['is_enable_magic_url']=$this->postInfo['enable_magic_url'] ? '' : 'checked="checked"';
            $this->data['is_enable_sig']=$this->postInfo['enable_sig'] ? 'checked="checked"' : '';
            $this->data['is_reply_notify']=$this->postInfo['topic_id'] ? 'checked="checked"' : '';
        }

        //load poll details when edit a topic
        if(isset($this->request->post['submit'])){
            $this->data['poll_question']=$this->request->post['poll-question'];
            $this->data['poll_option']=$this->request->post['poll-option'];
            $this->data['poll_option_per_user']=$this->request->post['poll-option-per-user'];
            $this->data['poll_run_day']=$this->request->post['poll-run-day'];
        }else{
            $pollOptions=$this->model_community_post->getPollOptions($this->request->get);

            $pollOptionStr='';

            foreach($pollOptions as $pollOption){
                $pollOptionStr.=$pollOption['poll_option_text'] . "\n";
            }

            $pollOptionStr=trim($pollOptionStr);

            $this->data['poll_question']=$topicResult['poll_title'];
            $this->data['poll_option']=$pollOptionStr;
            $this->data['poll_option_per_user']=($topicResult['poll_max_options']==0) ? '' : $topicResult['poll_max_options'];
            $this->data['poll_run_day']=(round($topicResult['poll_length']/(60*60*24))==0) ? '' : round($topicResult['poll_length']/(60*60*24));
        }

        $this->data['show_post_edit_reason']=TRUE;

        //detect enabled or disabled
        if($topicResult['topic_first_post_id']==$this->request->get['pid']){
            $this->data['topic_category_enabled']=$forumResult['topic_category_enabled'];
        }else{
            $this->data['topic_category_enabled']=FALSE;
        }

        if(($topicResult['topic_first_post_id']==$this->request->get['pid'])  && ($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['post_members_edit']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['post_members_edit']) || (!$this->postInfo['post_edit_locked'] && $this->postInfo['poster_id']==$this->customer->getForumUserId() && $userPermission['post_edit']))){
            $this->data['allow_poll']=TRUE;
        }else{
            $this->data['allow_poll']=FALSE;
        }

        if(($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['topic_featured']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['topic_featured']) && $topicResult['topic_first_post_id']==$this->request->get['pid'])){
            $this->data['allow_feature_topic']=TRUE;
        }else{
            $this->data['allow_feature_topic']=FALSE;
        }

        if(($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['topic_lock']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['topic_lock']) && $topicResult['topic_first_post_id']==$this->request->get['pid'])){
            $this->data['allow_lock_topic']=TRUE;
        }else{
            $this->data['allow_lock_topic']=FALSE;
        }

        if($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['edit_lock']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['edit_lock'])){
            $this->data['allow_lock_edit']=TRUE;
        }else{
            $this->data['allow_lock_edit']=FALSE;
        }

        if(($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['topic_sticky']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['topic_sticky']) && $topicResult['topic_first_post_id']==$this->request->get['pid'])){
            $this->data['allow_topic_sticky']=TRUE;
        }else{
            $this->data['allow_topic_sticky']=FALSE;
        }

        if(($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['topic_announcement']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['topic_announcement']) && $topicResult['topic_first_post_id']==$this->request->get['pid'])){
            $this->data['allow_topic_announcement']=TRUE;
        }else{
            $this->data['allow_topic_announcement']=FALSE;
        }

        if(($userPermission['role_id']==1 || ($userPermission['role_id']==2 && $userPermission['topic_global_announcement']) || ($userPermission['role_id']==3 && $userPermission['forum_mod_ids'] && in_array($this->customer->getForumUserId(),unserialize($userPermission['forum_mod_ids'])) && $userPermission['topic_global_announcement']) && $topicResult['topic_first_post_id']==$this->request->get['pid'])){
            $this->data['allow_topic_global_announcement']=TRUE;
        }else{
            $this->data['allow_topic_global_announcement']=FALSE;
        }

        //shared appendix
        $this->sharedAppendix();
    }

    public function captcha() {
        $this->load->library('captcha');

        $captcha = new Captcha();

        $this->session->data['captcha'] = $captcha->getCode();

        $captcha->showImage();
    }

    private function sharedAppendix(){
		if(file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/community/post.tpl')){
            $this->template=$this->config->get('config_template') . '/template/community/post.tpl';
		}else{
            $this->template='default/template/community/post.tpl';
		}	
			
        $this->children = array(
            'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header',
			'community/community_header'
		);
		
		$this->response->setOutput($this->render());         
    }

    private function sharedPrefix($forum_link,$forum_name,$action,$userPermission){
        $this->data['breadcrumbs']=array();
        
        $this->data['breadcrumbs'][]=array(
            'href'      =>      $this->url->link('common/home'),
            'text'      =>      $this->language->get('text_home'),
            'separator' =>      FALSE
        );
  
        $this->data['breadcrumbs'][]=array(
            'href'      =>      $this->url->link('community/community'),
            'text'      =>      $this->language->get('text_community'),
            'separator' =>      $this->language->get('text_separator')
        );      
        
        $this->data['breadcrumbs'][]=array(
            'href'      =>      $forum_link,
            'text'      =>      $forum_name,
            'separator' =>      $this->language->get('text_separator')
        );

        if($userPermission['posting_captcha']){
            $this->data['captcha_enabled']=TRUE;
        }else{
            $this->data['captcha_enabled']=FALSE;
        }

        $queryStrArr=explode('/',$this->request->server['QUERY_STRING']);
        $queryStrArr=explode('&',$queryStrArr[2]);

        $this->data['action']=$action;
        if($queryStrArr[0]!='edit'){
            $this->data['draft']=$this->model_community_post->getTotalDrafts($this->request->get);
        }

        //draft
        if(isset($this->request->post['loaddraft'])){
            $this->data['showDraft']=true;

            $drafts=$this->model_community_post->loadDrafts($this->request->get);
            $this->data['drafts']=array();

            if(isset($this->request->get['tid'])){
                foreach($drafts as $draft){
                    $this->data['drafts'][]=array(
                        'draft_subject_link'    =>     $this->url->link('community/post/reply') . '&fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid'] . '&did=' . $draft['draft_id'],
                        'draft_subject'         =>     $draft['draft_subject'],
                        'save_time'             =>     date($this->language->get('date_time_format_long'),$draft['save_time']),
                        'location'              =>     $draft['topic_title'],
                        'draft_location_link'   =>     $this->url->link('community/viewtopic') . '&fid=' . $this->request->get['fid'] . '&tid=' . $this->request->get['tid']
                    );
                }
            }else{
                foreach($drafts as $draft){
                    $this->data['drafts'][]=array(
                        'draft_subject_link'    =>     $this->url->link('community/post/topic') . '&fid=' . $draft['forum_id'] . '&did=' . $draft['draft_id'],
                        'draft_subject'         =>     $draft['draft_subject'],
                        'save_time'             =>     date($this->language->get('date_time_format_long'),$draft['save_time']),
                        'location'              =>     $draft['forum_name'],
                        'draft_location_link'   =>     $this->url->link('community/viewforum') . '&fid=' . $draft['forum_id']
                    );
                }
            }
        }else{
            $this->data['showDraft']=false;
        }

        if(isset($this->request->get['tid'])){
            $this->data['text_draft_location']=$this->language->get('text_draft_topic');
        }else{
            $this->data['text_draft_location']=$this->language->get('text_draft_forum');
        }

        $this->data['text_topic_cats']=$this->language->get('text_topic_cats');
        $this->data['text_draft_commentary_title']=$this->language->get('text_draft_commentary_title');
        $this->data['text_draft_commentary']=$this->language->get('text_draft_commentary');
        $this->data['text_draft_subject']=$this->language->get('text_draft_subject');
        $this->data['text_draft_time']=$this->language->get('text_draft_time');

        $this->data['text_poll_creation']=$this->language->get('text_poll_creation');
        $this->data['text_poll_tip']=$this->language->get('text_poll_tip');
        $this->data['text_poll_question']=$this->language->get('text_poll_question');
        $this->data['text_poll_option']=$this->language->get('text_poll_option');
        $this->data['text_poll_option_per_user']=$this->language->get('text_poll_option_per_user');
        $this->data['text_poll_option_per_user_tip']=$this->language->get('text_poll_option_per_user_tip');
        $this->data['text_poll_run_day']=$this->language->get('text_poll_run_day');
        $this->data['text_poll_run_day_tip']=$this->language->get('text_poll_run_day_tip');

        $this->data['entry_subject']=$this->language->get('entry_subject');
        $this->data['entry_captcha']=$this->language->get('entry_captcha');

        //button
        if(!isset($this->request->get['did'])){
            $this->data['text_loaddraft']=$this->language->get('text_loaddraft');
        }else{
            $this->data['text_loaddraft']=$this->language->get('text_reloaddraft');
        }

        $this->data['text_savedraft']=$this->language->get('text_savedraft');
        $this->data['text_submit']=$this->language->get('text_submit');

        if($this->config->get('bbcode_parsing_enable')){
            if($userPermission['posting_bbcode']){
                $bbButtonTag=$this->generateBbcode($userPermission);

                $this->data['bb_buttons']=$bbButtonTag[0];
                $this->data['bb_tags']=$bbButtonTag[1];

                $this->data['bbcode_enable']=TRUE;
            }else{
                $this->data['bbcode_enable']=FALSE;
            }
        }else{
            $this->data['bbcode_enable']=FALSE;
        }

        $this->data['smiles']=$this->generateSmiles($userPermission);

        //bbcode
        $this->data['text_font_color']=$this->language->get('text_font_color');
        $this->data['text_font_color_close']=$this->language->get('text_font_color_close');

        //bbcode enable or disable
        $this->data['text_bbcode_enabled']=$this->language->get('text_bbcode_enabled');
        $this->data['text_img_enabled']=$this->language->get('text_img_enabled');
        $this->data['text_flash_enabled']=$this->language->get('text_flash_enabled');
        $this->data['text_url_enabled']=$this->language->get('text_url_enabled');
        $this->data['text_smile_enabled']=$this->language->get('text_smile_enabled');
        $this->data['text_bbcode_disabled']=$this->language->get('text_bbcode_disabled');   
        $this->data['text_img_disabled']=$this->language->get('text_img_disabled');
        $this->data['text_flash_disabled']=$this->language->get('text_flash_disabled');
        $this->data['text_url_disabled']=$this->language->get('text_url_disabled');
        $this->data['text_smile_disabled']=$this->language->get('text_smile_disabled');
        //posting options
        $this->data['text_posting_options']=$this->language->get('text_posting_options');
        $this->data['text_disable_bbcode']=$this->language->get('text_disable_bbcode');
        $this->data['text_disable_smilies']=$this->language->get('text_disable_smilies');
        $this->data['text_not_parse_urls']=$this->language->get('text_not_parse_urls');
        $this->data['text_attach_signature']=$this->language->get('text_attach_signature');
        $this->data['text_notify_me']=$this->language->get('text_notify_me');
        $this->data['text_allow_feature_topic']=$this->language->get('text_allow_feature_topic');
        $this->data['text_allow_lock_topic']=$this->language->get('text_allow_lock_topic');
        $this->data['text_allow_lock_edit']=$this->language->get('text_allow_lock_edit');
        $this->data['text_set_topic_as']=$this->language->get('text_set_topic_as');
        $this->data['text_topic_normal']=$this->language->get('text_topic_normal');
        $this->data['text_topic_sticky']=$this->language->get('text_topic_sticky');
        $this->data['text_topic_announcement']=$this->language->get('text_topic_announcement');
        $this->data['text_topic_global_announcement']=$this->language->get('text_topic_global_announcement');
        $this->data['text_stick_topic_for']=$this->language->get('text_stick_topic_for');
        $this->data['text_post_edit_reason']=$this->language->get('text_post_edit_reason');
        $this->data['text_days']=$this->language->get('text_days');
        $this->data['text_stick_tip']=$this->language->get('text_stick_tip');

        //error
        if(isset($this->error['flood_limit'])){
            $this->data['error_flood_limit']=$this->error['flood_limit'];
        }else{
            $this->data['error_flood_limit']='';
        }

        if(isset($this->error['poll'])){
            $this->data['error_poll']=$this->error['poll'];
        }else{
            $this->data['error_poll']='';
        }

        if(isset($this->error['subject'])){
            $this->data['error_subject']=$this->error['subject'];
        }else{
            $this->data['error_subject']='';
        }

        if(isset($this->error['topic_cats'])){
            $this->data['error_topic_cats']=$this->error['topic_cats'];
        }else{
            $this->data['error_topic_cats']='';
        }

        if(isset($this->error['content'])){
            $this->data['error_content']=$this->error['content'];
        }else{
            $this->data['error_content']='';
        }

        if (isset($this->error['captcha'])) {
            $this->data['error_captcha'] = $this->error['captcha'];
        } else {
            $this->data['error_captcha'] = '';
        }

        //values stored in $_POST
        if(isset($this->request->post['subject'])){
            $this->data['subject']=$this->request->post['subject'];
        }elseif(isset($this->request->get['did'])){
            $temp=$this->model_community_post->loadDraft($this->request->get);
            $this->data['subject']=$temp['draft_subject'];
        }else{
            if($queryStrArr[0]=='reply'){
                $this->data['subject']=$this->language->get('text_re') . $this->topicInfo['topic_title'];
            }elseif($queryStrArr[0]=='edit'){
                $this->data['subject']=$this->postInfo['post_subject'];
            }else{
                $this->data['subject']='';
            }
        }

        if(isset($this->request->post['content'])){
            $this->data['content']=$this->request->post['content'];
            $this->data['is_edit_post']=false;
        }elseif(isset($this->request->get['did'])){
            $temp=$this->model_community_post->loadDraft($this->request->get);
            $this->data['content']=$temp['draft_contents'];
            $this->data['is_edit_post']=false;
        }elseif(isset($this->request->get['qpid'])){
            $temp='[quote]' . $this->model_community_post->loadQuotedContent($this->request->get) . '[/quote]';
            $this->data['content']=$temp;
            $this->data['is_edit_post']=true;
        }else{
            if($queryStrArr[0]=='edit'){
                $this->data['content']=$this->postInfo['post_text'];
                $this->data['is_edit_post']=true;// detect if the "save draft" button should be hidden
            }else{
                $this->data['content']='';
                $this->data['is_edit_post']=false;
            }
        }

        if (isset($this->request->post['captcha'])) {
            $this->data['captcha'] = $this->request->post['captcha'];
        } else {
            $this->data['captcha'] = '';
        }

        if(isset($this->request->post['submit'])){
            $this->data['is_feature_topic']=isset($this->request->post['feature_topic']) ? 'checked="checked"' : '';
            $this->data['is_lock_topic']=isset($this->request->post['lock_topic']) ? 'checked="checked"' : '';
            $this->data['is_lock_edit']=isset($this->request->post['lock_edit']) ? 'checked="checked"' : '';
            $this->data['stick_days']=isset($this->request->post['stick_topic_for']) ? $this->request->post['stick_topic_for'] :'';
            $this->data['post_edit_reason']=isset($this->request->post['post_edit_reason']) ? $this->request->post['post_edit_reason'] :'';

            if(isset($this->request->post['set_topic_as'])){
                switch($this->request->post['set_topic_as']){
                    case '1':
                        $this->data['is_set_topic_as_normal']='';
                        $this->data['is_set_topic_as_sticky']='checked="checked"';
                        $this->data['is_set_topic_as_announce']='';
                        $this->data['is_set_topic_as_global_announce']='';
                        break;
                    case '2':
                        $this->data['is_set_topic_as_normal']='';
                        $this->data['is_set_topic_as_sticky']='';
                        $this->data['is_set_topic_as_announce']='checked="checked"';
                        $this->data['is_set_topic_as_global_announce']='';
                        break;
                    case '3':
                        $this->data['is_set_topic_as_normal']='';
                        $this->data['is_set_topic_as_sticky']='';
                        $this->data['is_set_topic_as_announce']='';
                        $this->data['is_set_topic_as_global_announce']='checked="checked"';
                        break;
                    default:
                        $this->data['is_set_topic_as_normal']='checked="checked"';
                        $this->data['is_set_topic_as_sticky']='';
                        $this->data['is_set_topic_as_announce']='';
                        $this->data['is_set_topic_as_global_announce']='';
                }
            }else{
                $this->data['is_set_topic_as_normal']='checked="checked"';
                $this->data['is_set_topic_as_sticky']='';
                $this->data['is_set_topic_as_announce']='';
                $this->data['is_set_topic_as_global_announce']='';
            }
        }else{
            $this->data['is_feature_topic']=isset($this->topicInfo['topic_featured']) && $this->topicInfo['topic_featured'] ? 'checked="checked"' : '';
            $this->data['is_lock_topic']=isset($this->topicInfo['topic_status']) && $this->topicInfo['topic_status'] ? 'checked="checked"' : '';
            $this->data['is_lock_edit']=isset($this->postInfo['post_edit_locked']) && $this->postInfo['post_edit_locked'] ? 'checked="checked"' : '';
            $this->data['stick_days']=isset($this->topicInfo['topic_time_limit']) && $this->topicInfo['topic_time_limit'] ? round(($this->topicInfo['topic_time_limit']-time())/(60*60*24)) : '';
            $this->data['post_edit_reason']=isset($this->postInfo['post_edit_reason']) && $this->postInfo['post_edit_reason'] ? $this->postInfo['post_edit_reason'] : '';

            if(isset($this->topicInfo['topic_type'])){
                switch($this->topicInfo['topic_type']){
                    case '1':
                        $this->data['is_set_topic_as_normal']='';
                        $this->data['is_set_topic_as_sticky']='checked="checked"';
                        $this->data['is_set_topic_as_announce']='';
                        $this->data['is_set_topic_as_global_announce']='';
                        break;
                    case '2':
                        $this->data['is_set_topic_as_normal']='';
                        $this->data['is_set_topic_as_sticky']='';
                        $this->data['is_set_topic_as_announce']='checked="checked"';
                        $this->data['is_set_topic_as_global_announce']='';
                        break;
                    case '3':
                        $this->data['is_set_topic_as_normal']='';
                        $this->data['is_set_topic_as_sticky']='';
                        $this->data['is_set_topic_as_announce']='';
                        $this->data['is_set_topic_as_global_announce']='checked="checked"';
                        break;
                    default:
                        $this->data['is_set_topic_as_normal']='checked="checked"';
                        $this->data['is_set_topic_as_sticky']='';
                        $this->data['is_set_topic_as_announce']='';
                        $this->data['is_set_topic_as_global_announce']='';
                }
            }else{
                $this->data['is_set_topic_as_normal']='checked="checked"';
                $this->data['is_set_topic_as_sticky']='';
                $this->data['is_set_topic_as_announce']='';
                $this->data['is_set_topic_as_global_announce']='';
            }
        }
    }

    private function generateBbcode($userPermission){
        $bbButtons=array();
        $bbtags=array();

        $bbButtons[]='<select id="font-size" name="font-size" title="' . $this->language->get('text_font_size') . '" onchange="bbfontstyle(\'[size=\' + this.options[this.selectedIndex].value + \']\', \'[/size]\');this.selectedIndex = 2;">
                            <option value="50">' . $this->language->get('text_tiny') . '</option>
                            <option value="85">' . $this->language->get('text_small') . '</option>
                            <option selected="selected" value="100">' . $this->language->get('text_normal') . '</option>
                            <option value="150">' . $this->language->get('text_large') . '</option>
                            <option value="200">' . $this->language->get('text_huge') . '</option>
                          </select>';
        $bbtags[]="'[size=]'";
        $bbtags[]="'[/size]'";

        $bbButtons[]='<input type="button" id="bold_text" title="' . $this->language->get('text_bold_text') . '" onclick="bbstyle(2)">';
        $bbtags[]="'[b]'";
        $bbtags[]="'[/b]'";

        $bbButtons[]='<input type="button" id="italic_text" title="' . $this->language->get('text_italic_text') . '" onclick="bbstyle(4)">';
        $bbtags[]="'[i]'";
        $bbtags[]="'[/i]'";

        $bbButtons[]='<input type="button" id="underline_text" title="' . $this->language->get('text_underline_text') . '" onclick="bbstyle(6)">';
        $bbtags[]="'[u]'";
        $bbtags[]="'[/u]'";

        //no bbtags for font color button, has been added in the editor.js file
        $bbButtons[]='<input type="button" id="font_color" title="' . $this->language->get('text_font_color') . '" onclick="change_palette(\'font_colour_palette\');">';

        $bbButtons[]='<input type="button" id="quote_text" title="' . $this->language->get('text_quote_text') . '" onclick="bbstyle(8)">';
        $bbtags[]="'[quote]'";
        $bbtags[]="'[/quote]'";

        $bbButtons[]='<input type="button" id="code_display" title="' . $this->language->get('text_code_display') . '" onclick="bbstyle(10)">';
        $bbtags[]="'[code]'";
        $bbtags[]="'[/code]'";

        if($this->config->get('bbcode_url_parsing_enable')){
            if($userPermission['posting_bbcode_url']){
                $bbButtons[]='<input type="button" id="insert_URL" title="' . $this->language->get('text_insert_URL') . '" onclick="bbstyle(12)">';

                $this->data['bbcode_url_enable']=TRUE;
            }else{
                $this->data['bbcode_url_enable']=FALSE;
            }
        }else{
            $this->data['bbcode_url_enable']=FALSE;
        }
        $bbtags[]="'[url]'";
        $bbtags[]="'[/url]'";

        if($this->config->get('bbcode_img_parsing_enable')){
            if($userPermission['posting_bbcode_img']){
                $bbButtons[]='<input type="button" id="insert_image" title="' . $this->language->get('text_insert_image') . '" onclick="bbstyle(14)">';
                $this->data['bbcode_img_enable']=TRUE;
            }else{
                $this->data['bbcode_img_enable']=FALSE;
            }

        }else{
            $this->data['bbcode_img_enable']=FALSE;
        }
        $bbtags[]="'[img]'";
        $bbtags[]="'[/img]'";

        if($this->config->get('bbcode_flash_parsing_enable')){
            if($userPermission['posting_bbcode_flash']){
                $bbButtons[]='<input type="button" id="insert_flash" title="' . $this->language->get('text_insert_flash') . '" onclick="bbstyle(16)">';
                $this->data['bbcode_flash_enable']=TRUE;
            }else{
                $this->data['bbcode_flash_enable']=FALSE;
            }

        }else{
            $this->data['bbcode_flash_enable']=FALSE;
        }
        $bbtags[]="'[flash]'";
        $bbtags[]="'[/flash]'";

        $bbButtons[]='<input type="button" id="insert_youtube" title="' . $this->language->get('text_insert_youtube') . '" onclick="bbstyle(18)">';
        $bbtags[]="'[youtube]'";
        $bbtags[]="'[/youtube]'";

        $bbButtons[]='<input type="button" id="story_display" title="' . $this->language->get('text_story_display') . '" onclick="bbstyle(20)">';
        $bbtags[]="'[story]'";
        $bbtags[]="'[/story]'";

        return array($bbButtons,$bbtags);
    }

    private function generateSmiles($userPermission){
        if($this->config->get('smiles_parsing_enable')){
            if($userPermission['posting_smiles']){
                $smileStr='';

                $smileStr.='<img onclick="insert_text(\':D\', true); return false;" src="catalog/view/theme/default/image/smilies/01_very_happy.gif" alt=":D" title="' . $this->language->get('text_very_happy') . '" />';
                $smileStr.='<img onclick="insert_text(\':)\', true); return false;" src="catalog/view/theme/default/image/smilies/02_smile.gif" alt=":)" title="' . $this->language->get('text_smile') . '" />';
                $smileStr.='<img onclick="insert_text(\';)\', true); return false;" src="catalog/view/theme/default/image/smilies/03_wink.gif" alt=";)" title="' . $this->language->get('text_wink') . '" />';
                $smileStr.='<img onclick="insert_text(\':(\', true); return false;" src="catalog/view/theme/default/image/smilies/04_sad.gif" alt=":(" title="' . $this->language->get('text_sad') . '" />';
                $smileStr.='<img onclick="insert_text(\':o\', true); return false;" src="catalog/view/theme/default/image/smilies/05_surprised.gif" alt=":o" title="' . $this->language->get('text_surprised') . '" />';
                $smileStr.='<img onclick="insert_text(\':shock:\', true); return false;" src="catalog/view/theme/default/image/smilies/06_shocked.gif" alt=":shock:" title="' . $this->language->get('text_shocked') . '" />';
                $smileStr.='<img onclick="insert_text(\':?\', true); return false;" src="catalog/view/theme/default/image/smilies/07_confused.gif" alt=":?" title="' . $this->language->get('text_confused') . '" />';
                $smileStr.='<img onclick="insert_text(\'8-)\', true); return false;" src="catalog/view/theme/default/image/smilies/08_cool.gif" alt="8-)" title="' . $this->language->get('text_cool') . '" />';
                $smileStr.='<img onclick="insert_text(\':lol:\', true); return false;" src="catalog/view/theme/default/image/smilies/09_langhing.gif" alt=":lol:" title="' . $this->language->get('text_langhing') . '" />';
                $smileStr.='<img onclick="insert_text(\':x\', true); return false;" src="catalog/view/theme/default/image/smilies/10_mad.gif" alt=":x" title="' . $this->language->get('text_mad') . '" />';
                $smileStr.='<img onclick="insert_text(\':P\', true); return false;" src="catalog/view/theme/default/image/smilies/11_razz.gif" alt=":P" title="' . $this->language->get('text_razz') . '" />';
                $smileStr.='<img onclick="insert_text(\':-oops:\', true); return false;" src="catalog/view/theme/default/image/smilies/12_embarrassed.gif" alt=":-oops:" title="' . $this->language->get('text_embarrassed') . '" />';
                $smileStr.='<img onclick="insert_text(\':cry:\', true); return false;" src="catalog/view/theme/default/image/smilies/13_cry.gif" alt=":cry:" title="' . $this->language->get('text_cry') . '" />';
                $smileStr.='<img onclick="insert_text(\':evil:\', true); return false;" src="catalog/view/theme/default/image/smilies/14_evil_or_very_mad.gif" alt=":evil:" title="' . $this->language->get('text_evil_or_very_mad') . '" />';
                $smileStr.='<img onclick="insert_text(\':twisted:\', true); return false;" src="catalog/view/theme/default/image/smilies/15_twisted_evil.gif" alt=":twisted:" title="' . $this->language->get('text_twisted_evil') . '" />';
                $smileStr.='<img onclick="insert_text(\':roll:\', true); return false;" src="catalog/view/theme/default/image/smilies/16_rolling_eyes.gif" alt=":roll:" title="' . $this->language->get('text_rolling_eyes') . '" />';
                $smileStr.='<img onclick="insert_text(\':!:\', true); return false;" src="catalog/view/theme/default/image/smilies/17_exclamation.gif" alt=":!:" title="' . $this->language->get('text_exclamation') . '" />';
                $smileStr.='<img onclick="insert_text(\':-?:\', true); return false;" src="catalog/view/theme/default/image/smilies/18_question.gif" alt=":-?:" title="' . $this->language->get('text_question') . '" />';
                $smileStr.='<img onclick="insert_text(\':idea:\', true); return false;" src="catalog/view/theme/default/image/smilies/19_idea.gif" alt=":idea:" title="' . $this->language->get('text_idea') . '" />';
                $smileStr.='<img onclick="insert_text(\':arrow:\', true); return false;" src="catalog/view/theme/default/image/smilies/20_arrow.gif" alt=":arrow:" title="' . $this->language->get('text_arrow') . '" />';
                $smileStr.='<img onclick="insert_text(\':|\', true); return false;" src="catalog/view/theme/default/image/smilies/21_neutral.gif" alt=":|" title="' . $this->language->get('text_neutral') . '" />';
                $smileStr.='<img onclick="insert_text(\':mrgreen:\', true); return false;" src="catalog/view/theme/default/image/smilies/22_mr._green.gif" alt=":mrgreen:" title="' . $this->language->get('text_mr_green') . '" />';
                $smileStr.='<img onclick="insert_text(\':geek:\', true); return false;" src="catalog/view/theme/default/image/smilies/23_geek.gif" alt=":geek:" title="' . $this->language->get('text_geek') . '" />';
                $smileStr.='<img onclick="insert_text(\':ugeek:\', true); return false;" src="catalog/view/theme/default/image/smilies/24_uber_geek.gif" alt=":ugeek:" title="' . $this->language->get('text_uber_geek') . '" />';

                $this->data['smiles_enable']=TRUE;

                return $smileStr;
            }else{
                $this->data['smiles_enable']=FALSE;
            }
        }else{
            $this->data['smiles_enable']=FALSE;
        }
    }

//to-do: add (1) $_congif['max_subject_length'](varchar, default value is 100, namely 50 characters, suggest others to use this length at admin! (2)$_congif['max_content_length'](text, default values is 10000, namly 5000 characters, suggest others to use this length at admin!  )
    private function validate($userPermission){
        if(utf8_strlen($this->request->post['subject']) > floor($this->config->get('posting_max_subject_length'))){
            $this->error['subject']=sprintf($this->language->get('error_subject'),floor($this->config->get('posting_max_subject_length')));
        }

        $queryStrArr=explode('/',$this->request->server['QUERY_STRING']);
        $queryStrArr=explode('&',$queryStrArr[2]);

        if($queryStrArr[0]=='topic' || $queryStrArr[0]=='edit'){
            if(isset($this->request->post['topic-category']) && $this->request->post['topic-category']==$this->language->get('text_topic_cats')){
                $this->error['topic_cats']=$this->language->get('error_empty_topic_cats');
            }
        }

        if($this->request->post['subject']==''){
            $this->error['subject']=$this->language->get('error_empty_subject');
        }

        if(utf8_strlen($this->request->post['content']) > floor($this->config->get('posting_max_content_length'))){
            $this->error['content']=sprintf($this->language->get('error_content'),floor($this->config->get('posting_max_content_length')));
        }

        if($this->request->post['content']==''){
            $this->error['content']=$this->language->get('error_empty_content');
        }

        if($userPermission['posting_captcha']){
            if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
                $this->error['captcha'] = $this->language->get('error_captcha');
            }
        }

        if(!$userPermission['user_ignore_flood_limit']){
            if(isset($this->session->data['previous_post_time']) && (time()-$this->session->data['previous_post_time']<=$this->config->get('flood_limit'))){
                $this->error['flood_limit'] = $this->language->get('error_flood_limit');
            }
        }

        //poll
        if(isset($this->request->post['poll-question'])){
            if($this->request->post['poll-question']!='' && (trim($this->request->post['poll-option'])=='' || $this->request->post['poll-option-per-user']=='')){
                $this->error['poll'] = $this->language->get('error_empty_poll_entity');
            }

            if(trim($this->request->post['poll-option'])!='' && ($this->request->post['poll-question']=='' || $this->request->post['poll-option-per-user']=='')){
                $this->error['poll'] = $this->language->get('error_empty_poll_entity');
            }

            if($this->request->post['poll-option-per-user']!='' && (trim($this->request->post['poll-option'])=='' || $this->request->post['poll-question']=='')){
                $this->error['poll'] = $this->language->get('error_empty_poll_entity');
            }

            if(($this->request->post['poll-run-day']>=0 && $this->request->post['poll-run-day']!='') && ($this->request->post['poll-question']=='' || trim($this->request->post['poll-option'])=='' || $this->request->post['poll-option-per-user']=='')){
                $this->error['poll'] = $this->language->get('error_empty_poll_entity');
            }

            if(utf8_strlen($this->request->post['poll-question']) > floor($this->config->get('posting_max_subject_length'))){
                $this->error['poll']=sprintf($this->language->get('error_poll_question'),floor($this->config->get('posting_max_subject_length')));
            }

            if($this->request->post['poll-option-per-user']!=''  && $this->request->post['poll-option-per-user']<=0){
                $this->error['poll'] = $this->language->get('error_poll_option_per_user');
            }

            if($this->request->post['poll-run-day']<0){
                $this->error['poll'] = $this->language->get('error_poll_run_day');
            }
        }

        if(!$this->error){
            return true;
        }else{
            return false;
        }
    }
}
?>